package leetcode.f1t100;

import java.util.Stack;

/**
 * 格式化文件路径
 * https://leetcode.com/problems/simplify-path/
 *
 * @Author lichongxiao
 * @Date 2022/11/23 16:32
 */
public class Q71_SimplifyPath {
    public String simplifyPath(String path) {
        Stack<String> stack = new Stack<>();
        String[] splits = path.split("/");
        for (String split : splits) {
            if ("..".equals(split)) {
                if (!stack.isEmpty()) {
                    stack.pop();
                }
                continue;
            } else if (".".equals(split) || "".equals(split)) {
                continue;
            } else {
                stack.push(split);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (String str : stack.toArray(new String[0])) {
            sb.append("/").append(str);
        }
        String ans = sb.toString();
        return ans.length() == 0 ? "/" : ans;
    }


    public static void main(String[] args) {
        Q71_SimplifyPath bean = new Q71_SimplifyPath();
        System.out.println(bean.simplifyPath("/../"));
    }
}
